package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConst;
import com.itheima.constant.RedisConst;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Reference
    SetmealService setmealService;

    @Autowired
    JedisPool jedisPool;

    @RequestMapping("/getSetmeal")
    public Result getSetmeal() {
        log.debug("SetmealController： getSetmeal");
        //先查redis中的数据
        Jedis jedis = jedisPool.getResource();
        //从redis中查询
        String listRedis = jedis.get(RedisConst.SETMEAL_ALL_RESOURCES);
        //获得JSON字符，先转化成JSON对象，再转化成对象返回前端
        List<Setmeal> parse = (List<Setmeal>)JSON.parse(listRedis);
        //判断
        if ( parse == null||parse.size() == 0 ) {
            //如果没数据，查询MySQL
            List<Setmeal> setmealList = setmealService.findAll();
            log.debug("查询所有套餐成功！！");
            log.debug(setmealList.toString());
            //将数据返回，并存到redis中,转化到json字符串。
            String jsonSetmeal = JSON.toJSONString(setmealList);
            //把JSON字符串写入到redis中
            jedis.set(RedisConst.SETMEAL_ALL_RESOURCES, jsonSetmeal);

            return new Result(true, MessageConst.QUERY_SETMEALLIST_SUCCESS, setmealList);
        }

        return new Result(true, MessageConst.QUERY_SETMEALLIST_SUCCESS, parse);
    }
    @RequestMapping("/findDetailsById")
    //检查组信息
    public Result findDetailsById(Integer id) {
        log.debug("SetmealController:findDetailById: " + id);
        //在redis中查询检查组
        Jedis jedis = jedisPool.getResource();
        //获得查询redis中的Key，根据Key查询
        String key = RedisConst.GROUP_ONE_RESOURCES + String.valueOf(id);
        //获得一个JSON字符串
        String redisSetmeal = jedis.get(key);
        //转化成JSON类型的对象
        JSONObject jsonSetmeal = (JSONObject) JSON.parse(redisSetmeal);

        //判断如果为空，则查询MySQL
        if (jsonSetmeal == null ) {
            Setmeal setmeal = setmealService.findDetailById(id);
            log.debug("查询套餐详情成功");
            log.debug(setmeal.toString());
            //查询完成后需要存到redis中
            String Jsonsetmeal = JSON.toJSONString(setmeal);

            jedis.set(key, Jsonsetmeal);

            return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, setmeal);
        }
        //把JSON对象转化为实体类对象，返回前端
        Setmeal setmealRedis = jsonSetmeal.toJavaObject(Setmeal.class);
        return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, setmealRedis);

    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug("SetmealController:findById: " + id);
        Setmeal setmeal = setmealService.findById(id);
        log.debug("根据id查询套餐成功");
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,setmeal);
    }
}
